<?php

///////////////////////////////////////////////////////////////////////////////
/**
 * Include this file in your application to use the Connector library.
 *
 * Normally, there is no need to include any other files from the Connector
 * library. Call the static {@link ConnectorFactory::create()} method to get
 * an IConnector instance with an open database connection.
 *
 * System requirements:
 * <ul>
 * <li>PHP 5</li>
 * <li>The supported PHP database extensions.</li>
 * </ul>
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * The Connector library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * {@link http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License}
 * for more details.
 *
 * @author Per Egil Roksvaag
 * @copyright 2009 Per Egil Roksvaag
 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
 * @package connector
 * @version 2.0.0
 */

///////////////////////////////////////////////////////////////////////////////
/**
 * Include the IConnector interface.
 */

require_once("IConnector.php");

///////////////////////////////////////////////////////////////////////////////
/**
 * Include this file in your application to use the Connector library.
 *
 * Normally, there is no need to include any other files from the Connector
 * library. Call the static {@link ConnectorFactory::create()} method to get
 * an IConnector instance with an open database connection.
 *
 * @package connector
 */

class ConnectorFactory
{
	///////////////////////////////////////////////////////////////////////////
	/**
	 * @var array An associated array of initialized IConnector instances.
	 */

	static private $cache = array();

	///////////////////////////////////////////////////////////////////////////
	/**
	 * Include and create an instance of the selected IConnector class.
	 *
	 * If an IConnector instance with identical classname and connection
	 * parameters already exists, the existing instance is returned. If the
	 * classname is unknown or an error occurs, null is returend.
	 *
	 * IMPORTANT: Only use IConnector constants in the <var>$connection</var>
	 * and <var>$options</var> parameters. <b>No native constants</b> are
	 * available before this function has completed.
	 *
	 * @param string $classname The name of the IConnector class to use as a database connector.
	 * @param array $connection An associated array of connection settings.
	 * @param array $options An associated array of global options.
	 * @return IConnector A new or existing IConnector instance with an open database connection.
	 */

	static public function create($classname, $connection, $options = array())
	{
		$hash = md5($classname.serialize($connection));

		if(array_key_exists($hash, self::$cache))
		{
			$instance = self::$cache[$hash];
			$instance->setOptions($options);
			return $instance;
		}
		switch($classname)
		{
			case "PdoConnector": require_once("PdoConnector.php"); break;
			case "OdbcConnector": require_once("OdbcConnector.php"); break;
			case "MysqlConnector": require_once("MysqlConnector.php"); break;
			case "MssqlConnector": require_once("MssqlConnector.php"); break;
			case "SqlsrvConnector": require_once("SqlsrvConnector.php"); break;
			default: return null;
		}
		$instance = new $classname($connection, $options);

		if($instance->getLink())
		{
			self::$cache[$hash] = $instance;
			return $instance;
		}
		return null;
	}

	///////////////////////////////////////////////////////////////////////////
}

?>